package com.tanhua.dubbodb.api;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbodb.mapper.UserInfoMapper;
import com.tanhua.dubbointerface.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    /**
     * 保存用户资料
     * @param userinfo
     */
    @Autowired
    private UserInfoMapper userInfoMapper;
    /**
     * 保存用户资料
     * @param userinfo
     */
    @Override
    public void save(UserInfo userinfo) {

        userInfoMapper.insert(userinfo);
    }

    /**
     * 更新用户资料
     * @param userInfo
     */
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 通过用户id查找用户资料
     * @param userId
     * @return
     */
    @Override
    public UserInfo findUserInfoById(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    /**
     * 通过用户id集合查找对应的用户资料信息，封装为map返回

     * @return
     */
    @Override
    public Map<Long, UserInfo> findUserInfoByIds(List<Long> userIds, UserInfo info) {
//        List<Integer> id =ids.stream().map(item->{
//            return item.intValue();
//        }).collect(Collectors.toList());
////        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
////        queryWrapper.in("id",ids);
//        Map<Long, UserInfo> map = new HashMap<>();
//        List<UserInfo> list = userInfoMapper.selectBatchIds(id);
//        for (UserInfo info : list) {
//            map.put(userInfo.getId(),userInfo);
//        }
        QueryWrapper qw = new QueryWrapper();
        //1、用户id列表
        qw.in("id",userIds);
        //2、添加筛选条件
        if(info != null) {
            if(info.getAge() != null) {
                qw.lt("age",info.getAge());
            }
            if(!StringUtils.isEmpty(info.getGender())) {
                qw.eq("gender",info.getGender());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }
}
